<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_jadwal_pmb extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'PMBPeriodID';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('*');
			$this->db->from('pmb_jadwal a');			
			
			$this->db->where('a.status', "A");
			
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function getdataedit($kode){
		$this->db->flush_cache();
		$this->db->select('*');
		$this->db->from('pmb_jadwal');
		$this->db->where('PMBPeriodID', $kode);
		
		return $this->db->get();
	}
	
	function togrid($data, $count){
		$response->total = $count;
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
		}else{
		$response->rows = array();
		}
		return json_encode($response);
	}
	
	function isExistKode($kode){
		if ($kode!=null)
			$this->db->where('PMBPeriodID',$kode);
		
		$this->db->select('*');
		$this->db->from('pmb_jadwal');
		$query = $this->db->get();
		
		$rs = $query->num_rows() ;		
		$query->free_result();
		
		return ($rs>0);
	}
	
	function InsertOnDb($data){
		//query insert data		
		$this->db->flush_cache();
		$this->db->set('PMBPeriodID', $data['PMBPeriodID']);
        $this->db->set('kd_pt', $data['kd_pt']);
        $this->db->set('Nama', $data['Nama']);
        $this->db->set('TglMulai', FormatDateToMysql($data['TglMulai']));
        $this->db->set('TglSelesai', FormatDateToMysql($data['TglSelesai']));
        $this->db->set('UjianMulai', FormatDateToMysql($data['UjianMulai']));
        $this->db->set('UjianSelesai', FormatDateToMysql($data['UjianSelesai']));
        $this->db->set('BayarMulai', FormatDateToMysql($data['BayarMulai']));
        $this->db->set('BayarSelesai', FormatDateToMysql($data['BayarSelesai']));
		
		$tmp_TelitiBayarProdi = "";
		if(isset($data['TelitiBayarProdi1'])){$tmp_TelitiBayarProdi .= "Agribisnis, ";}
		if(isset($data['TelitiBayarProdi2'])){$tmp_TelitiBayarProdi .= "Agro Eko Teknologi, ";}
		if(isset($data['TelitiBayarProdi3'])){$tmp_TelitiBayarProdi .= "Akutansi, ";}
		if(isset($data['TelitiBayarProdi4'])){$tmp_TelitiBayarProdi .= "Biologi, ";}
		if(isset($data['TelitiBayarProdi5'])){$tmp_TelitiBayarProdi .= "Ekonomi Pembangunan, ";}
		if(isset($data['TelitiBayarProdi6'])){$tmp_TelitiBayarProdi .= "Geologi, ";}
		if(isset($data['TelitiBayarProdi7'])){$tmp_TelitiBayarProdi .= "Kehutanan, ";}
		if(isset($data['TelitiBayarProdi8'])){$tmp_TelitiBayarProdi .= "Manejmen, ";}
		if(isset($data['TelitiBayarProdi9'])){$tmp_TelitiBayarProdi .= "Manejemen Sumberdaya Pesisir, ";}
		if(isset($data['TelitiBayarProdi10'])){$tmp_TelitiBayarProdi .= "Sistem Informasi, ";}
		
		$this->db->set('TelitiBayarProdi', $tmp_TelitiBayarProdi);
		
		$this->db->set('CUID', $this->session->userdata('user_id'));
        $this->db->set('CDATE', date('Y-m-d H:i:s'));
		
        $this->db->set('status', 'A');
		
		$result = $this->db->insert('pmb_jadwal');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function UpdateOnDb($data){
		//query insert data		
		$this->db->flush_cache();
		$this->db->set('PMBPeriodID', $data['PMBPeriodID']);
        $this->db->set('kd_pt', $data['kd_pt']);
        $this->db->set('Nama', $data['Nama']);
        $this->db->set('TglMulai', FormatDateToMysql($data['TglMulai']));
        $this->db->set('TglSelesai', FormatDateToMysql($data['TglSelesai']));
        $this->db->set('UjianMulai', FormatDateToMysql($data['UjianMulai']));
        $this->db->set('UjianSelesai', FormatDateToMysql($data['UjianSelesai']));
        $this->db->set('BayarMulai', FormatDateToMysql($data['BayarMulai']));
        $this->db->set('BayarSelesai', FormatDateToMysql($data['BayarSelesai']));
		//$this->db->set('TelitiBayarProdi', isset($data['TelitiBayarProdi'])?	'Agribisnis':'Agro Eko Teknologi');
		
		$tmp_TelitiBayarProdi = "";
		if(isset($data['TelitiBayarProdi1'])){$tmp_TelitiBayarProdi .= "Agribisnis, ";}
		if(isset($data['TelitiBayarProdi2'])){$tmp_TelitiBayarProdi .= "Agro Eko Teknologi, ";}
		if(isset($data['TelitiBayarProdi3'])){$tmp_TelitiBayarProdi .= "Akutansi, ";}
		if(isset($data['TelitiBayarProdi4'])){$tmp_TelitiBayarProdi .= "Biologi, ";}
		if(isset($data['TelitiBayarProdi5'])){$tmp_TelitiBayarProdi .= "Ekonomi Pembangunan, ";}
		if(isset($data['TelitiBayarProdi6'])){$tmp_TelitiBayarProdi .= "Geologi, ";}
		if(isset($data['TelitiBayarProdi7'])){$tmp_TelitiBayarProdi .= "Kehutanan, ";}
		if(isset($data['TelitiBayarProdi8'])){$tmp_TelitiBayarProdi .= "Manejmen, ";}
		if(isset($data['TelitiBayarProdi9'])){$tmp_TelitiBayarProdi .= "Manejemen Sumberdaya Pesisir, ";}
		if(isset($data['TelitiBayarProdi10'])){$tmp_TelitiBayarProdi .= "Sistem Informasi, ";}
		
		$this->db->set('TelitiBayarProdi', $tmp_TelitiBayarProdi);
		
		$this->db->set('MUID', $this->session->userdata('user_id'));
        $this->db->set('MDATE', date('Y-m-d H:i:s'));
		
        $this->db->set('status', isset($data['status'])?'A':'T');
		
		$this->db->where('PMBPeriodID', $data['kode']);
		$result = $this->db->update('pmb_jadwal');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	
	function DeleteOnDb($kode){
		//query delet data  dengan ganti status db            
		$this->db->flush_cache();
		
		$this->db->set('status', "T");
	   
		$this->db->set('MUID', $this->session->userdata('user_id'));
		$this->db->set('MDATE', date('Y-m-d H:i:s'));
		
		$this->db->where('PMBPeriodID', $kode);
		$result = $this->db->update('pmb_jadwal');
	   
	   
		//return
		if($result) {
				return TRUE;
		}else {
				return FALSE;
		}
	}
	
}

?>